//package com.zws.cucumber.spring.configuration;
//
//import com.zws.cucumber.optimization.ZwsOptimizations;
////import com.zws.cucumber.optimization.socket.connector.ZwsCallerExecutionJettyConnector;
////import com.zws.cucumber.optimization.socket.connector.ZwsConnectorJettyAdaptor;
////import com.zws.cucumber.optimization.socket.oio.ZwsSocketImplAdapter;
//import com.zws.cucumber.spring.jetty9.ZwsQuickResponseFilter;
//import org.eclipse.jetty.server.Connector;
//import org.eclipse.jetty.server.LocalConnector;
//import org.eclipse.jetty.server.NetworkTrafficServerConnector;
//import org.eclipse.jetty.server.Server;
//import org.eclipse.jetty.servlet.ListenerHolder;
//import org.eclipse.jetty.util.thread.QueuedThreadPool;
//import org.eclipse.jetty.webapp.WebAppContext;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.BeansException;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContextAware;
//import org.springframework.context.EnvironmentAware;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.env.Environment;
//import org.springframework.web.context.WebApplicationContext;
//
//import javax.servlet.DispatcherType;
//import javax.servlet.ServletContext;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.EnumSet;
//import java.util.List;
//
//import static com.zws.cucumber.ZwsConstants.JETTY_PORT;
//import static com.zws.cucumber.ZwsConstants.WEB_APP_LOCATION;
//
///**
// * author: zws
// */
//@Configuration
//public class ZwsJettyConf implements ApplicationContextAware, EnvironmentAware {
//
//    private static final Logger logger = LoggerFactory.getLogger(ZwsJettyConf.class);
//
//    private ApplicationContext ctx;
//
//    private Environment env;
//
//    @Bean(destroyMethod = "stop")
//    public Server jettyServer() throws Exception {
//        String contextPath = env.getProperty("server.servlet.context-path", "/");
//
//        boolean jettyMinThread = Boolean.TRUE.equals(env.getProperty("cucumber-ct.jetty.minThread", Boolean.class));
//        QueuedThreadPool threadPool = null;
//        if (jettyMinThread) {
//            threadPool = new QueuedThreadPool();
//            threadPool.setMinThreads(3);
//            threadPool.setMaxThreads(3);
//        }
//
//        Server jettyServer = threadPool == null ? new Server() : new Server(threadPool);
//        NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(jettyServer);
//        connector.setPort(JETTY_PORT);
//        connector.setReuseAddress(true);
//
////        LocalConnector localConnector;
//        ZwsConnectorJettyAdaptor connectorAdaptor;
//
////        LocalConnector localConnector = new LocalConnector(jettyServer);
////        ZwsSocketImplAdapter.registerLocalConnectorAdaptor(JETTY_PORT, new ZwsConnectorJettyAdaptor(localConnector));
//        List<Connector> connectorList = new ArrayList<>();
//        connectorList.add(connector);
//        if (ZwsOptimizations.isJettyExecutionInMainThreadEnabled()) {
//            LocalConnector localConnector = new LocalConnector(jettyServer);
//
//            ZwsCallerExecutionJettyConnector callerExecutionJettyLocalConnector = new ZwsCallerExecutionJettyConnector(jettyServer);
//            callerExecutionJettyLocalConnector.start();
//            connectorAdaptor = new ZwsConnectorJettyAdaptor(localConnector, callerExecutionJettyLocalConnector);
////            connectorAdaptor.setAcceptorRunnable(((ZwsCallerExecutionJettyConnector) localConnector).getAcceptorRunnable());
//
//            connectorList.add(localConnector);
//            connectorList.add(callerExecutionJettyLocalConnector);
//
//        } else {
//            LocalConnector localConnector = new LocalConnector(jettyServer);
//            connectorAdaptor = new ZwsConnectorJettyAdaptor(localConnector);
//
//            connectorList.add(localConnector);
//        }
//
//        ZwsSocketImplAdapter.registerLocalConnectorAdaptor(JETTY_PORT, connectorAdaptor);
////        jettyServer.setConnectors(new Connector[]{ connector, localConnector });
////        jettyServer.setConnectors(new Connector[]{ connector });
//        jettyServer.setConnectors(connectorList.toArray(new Connector[0]));
//
//        WebAppContext webAppContext = new WebAppContext(WEB_APP_LOCATION, contextPath) {
//            @Override public void startWebapp() throws Exception {
//                ListenerHolder[] origin = this.getServletHandler().getListeners();
//                ListenerHolder[] replaced = Arrays.stream(origin)
//                        .filter(h -> !h.getClassName().equals("org.springframework.web.context.ContextLoaderListener"))
//                        .toArray(ListenerHolder[]::new);
//                this.getServletHandler().setListeners(replaced);
//                logger.debug(Arrays.toString(origin));
//                logger.debug(Arrays.toString(replaced));
//                // inject ZwsQuickResponseFilter
//                logger.debug("inject ZwsQuickResponseFilter");
//                this.getServletHandler().addFilterWithMapping(ZwsQuickResponseFilter.class.getName(), "/*", EnumSet.of(DispatcherType.REQUEST));
//
//                super.startWebapp();
//            }
//        };
//        ServletContext servletContext = webAppContext.getServletContext();
//        //set reference to actual Spring ApplicationContext
//        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx);
//        jettyServer.setHandler(webAppContext);
//        jettyServer.setStopAtShutdown(true);
//        jettyServer.start();
//
//        /*
//         * Check whether the app is available
//         */
//        if (webAppContext.getUnavailableException() != null) {
//            throw new IllegalStateException(webAppContext.getUnavailableException());
//        }
//
//        return jettyServer;
//    }
//
//    @Override
//    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//        this.ctx = applicationContext;
//    }
//
//    @Override
//    public void setEnvironment(Environment environment) {
//        this.env = environment;
//    }
//
//}
